/***************************************************************************
 * 版权所有：深圳市一非科技有限公司
 * 文件名称：HandBlueMethod.m
 * 内容摘要：手环蓝牙辅助类
 * 编辑作者：彭于
 * 版本编号：1.0.1
 * 创建日期：2016年02月25日
 ***************************************************************************/

#import "HandBlueMethod.h"

@implementation HandBlueMethod

/***************************************************************************
 * 方法名称：getStepResultData
 * 功能描述：获取处理过的步行数据
 * 输入参数：
 * 返回数据：
 ***************************************************************************/
- (NSArray *)getStepResultData:(NSArray *)stepDataArr
{
    NSMutableArray *lastStepArray = [[NSMutableArray alloc] init];
    
    for (int i = 0; i < stepDataArr.count; i++)
    {
        NSDictionary *stepDataDic = [stepDataArr objectAtIndex:i];
        NSString *step1 = [stepDataDic objectForKey:@"step1"];
        NSString *step2 = [stepDataDic objectForKey:@"step2"];
        
        NSString *resultStep = nil;
        NSString *stepAll1 = [[step1 substringFromIndex:4] substringToIndex:step1.length-6];
        
        if (step2 != nil)
        {
            NSString *stepAll2 = [[step2 substringFromIndex:4] substringToIndex:step2.length-6];
            resultStep = [NSString stringWithFormat:@"%@%@",stepAll1,stepAll2];
        }
        else
        {
            resultStep = [NSString stringWithFormat:@"%@",stepAll1];
        }
        
        NSArray *testArr = [self getLastData:resultStep withMark:@"step"];
        [lastStepArray addObjectsFromArray:testArr];
    }
    
    return lastStepArray;
}

/***************************************************************************
 * 方法名称：getSleepResultData
 * 功能描述：获取处理过的睡眠数据
 * 输入参数：
 * 返回数据：
 ***************************************************************************/
- (NSArray *)getSleepResultData:(NSArray *)sleepDataArr
{
    NSMutableArray *lastSleepArray = [[NSMutableArray alloc] init];
    
    for (int i = 0; i < sleepDataArr.count; i++)
    {
        NSDictionary *stepDataDic = [sleepDataArr objectAtIndex:i];
        NSString *sleep1 = [stepDataDic objectForKey:@"sleep1"];
        NSString *sleep2 = [stepDataDic objectForKey:@"sleep2"];
        
        NSString *resultSleep = nil;
        NSString *sleepAll1 = [[sleep1 substringFromIndex:4] substringToIndex:sleep1.length-6];
        
        if (sleep2 != nil)
        {
            NSString *sleepAll2 = [[sleep2 substringFromIndex:4] substringToIndex:sleep2.length-6];
            resultSleep = [NSString stringWithFormat:@"%@%@",sleepAll1,sleepAll2];
        }
        else
        {
            resultSleep = [NSString stringWithFormat:@"%@",sleepAll1];
        }
        
        NSArray *testArr = [self getLastData:resultSleep withMark:@"sleep"];
        [lastSleepArray addObjectsFromArray:testArr];
    }
    
    return lastSleepArray;
}

/***************************************************************************
 * 方法名称：getXinLvResultData
 * 功能描述：获取处理过的心率数据
 * 输入参数：
 * 返回数据：
 ***************************************************************************/
- (NSArray *)getXinLvResultData:(NSArray *)xinLvDataArr
{
    NSMutableArray *lastXinLvArray = [[NSMutableArray alloc] init];
    
    for (int i = 0; i < xinLvDataArr.count; i++)
    {
        NSDictionary *stepDataDic = [xinLvDataArr objectAtIndex:i];
        NSString *xinLv1 = [stepDataDic objectForKey:@"xinLv1"];
        NSString *xinLv2 = [stepDataDic objectForKey:@"xinLv2"];
        NSString *xinLv3 = [stepDataDic objectForKey:@"xinLv3"];
        NSString *xinLv4 = [stepDataDic objectForKey:@"xinLv4"];
        
        NSString *resultSleep = nil;
        NSString *xinLvAll1 = [[xinLv1 substringFromIndex:4] substringToIndex:xinLv1.length-6];
        resultSleep = [NSString stringWithFormat:@"%@",xinLvAll1];
        
        if (xinLv2 != nil)
        {
            NSString *xinLvAll2 = [[xinLv2 substringFromIndex:4] substringToIndex:xinLv2.length-6];
            resultSleep = [NSString stringWithFormat:@"%@%@",xinLvAll1,xinLvAll2];
            
            if (xinLv3 != nil)
            {
                NSString *xinLvAll3 = [[xinLv3 substringFromIndex:4] substringToIndex:xinLv3.length-6];
                resultSleep = [NSString stringWithFormat:@"%@%@%@",xinLvAll1,xinLvAll2,xinLvAll3];
                
                if (xinLv4 != nil)
                {
                    NSString *xinLvAll4 = [[xinLv4 substringFromIndex:4] substringToIndex:xinLv4.length-6];
                    resultSleep = [NSString stringWithFormat:@"%@%@%@%@",xinLvAll1,xinLvAll2,xinLvAll3,xinLvAll4];
                }
            }
        }
        
        NSArray *testArr = [self getLastData:resultSleep withMark:@"xinLv"];
        [lastXinLvArray addObjectsFromArray:testArr];
    }
    
    return lastXinLvArray;
}

/***************************************************************************
 * 方法名称：getLastData
 * 功能描述：获取最后的手环数据
 * 输入参数：
 * 返回数据：
 ***************************************************************************/
- (NSArray *)getLastData:(NSString *)allString withMark:(NSString *)mark
{
    NSMutableArray *resultArr = [[NSMutableArray alloc] init];
    int stringLeng = (int)allString.length/2;
    
    int j=0;
    Byte bytes[stringLeng];
    
    for(int i=0;i<[allString length];i++)
    {
        int int_ch; //// 两位16进制数转化后的10进制数
        
        unichar hex_char1 = [allString characterAtIndex:i]; ////两位16进制数中的第一位(高位*16)
        int int_ch1;
        if(hex_char1 >= '0' && hex_char1 <='9')
            int_ch1 = (hex_char1-48)*16; //// 0 的Ascll - 48
        else if(hex_char1 >= 'A' && hex_char1 <='F')
            int_ch1 = (hex_char1-55)*16; //// A 的Ascll - 65
        else
            int_ch1 = (hex_char1-87)*16; //// a 的Ascll - 97
        i++;
        
        unichar hex_char2 = [allString characterAtIndex:i]; ///两位16进制数中的第二位(低位)
        int int_ch2;
        if(hex_char2 >= '0' && hex_char2 <='9')
            int_ch2 = (hex_char2-48); //// 0 的Ascll - 48
        else if(hex_char2 >= 'A' && hex_char2 <='F')
            int_ch2 = hex_char2-55; //// A 的Ascll - 65
        else
            int_ch2 = hex_char2-87; //// a 的Ascll - 97
        
        int_ch = int_ch1+int_ch2;
        //        NSLog(@"int_ch=%d",int_ch);
        bytes[j] = int_ch; ///将转化后的数放入Byte数组里
        j++;
    }
    
    NSDateFormatter *myFormatter = [[NSDateFormatter alloc] init];
    [myFormatter setDateFormat: @"yyyy-MM-dd HH:mm:ss"];
    
    int c0 = bytes[0]&0xFF;
    int c1 = bytes[1]&0xFF;
    int c2 = bytes[2]&0xFF;
    int c3 = bytes[3]&0xFF;
    int juTime = [self getNowDateFromatAnDate:[NSDate date]];
    NSString *timeMinutes = [NSString stringWithFormat:@"%d",c3+(c2<<8)+(c1<<16)+(c0<<24)-juTime];
    NSDate *handTime = [NSDate dateWithTimeIntervalSince1970:[timeMinutes longLongValue]];
    
    if ([mark isEqualToString:@"step"])
    {
        NSMutableDictionary *resultDic = [[NSMutableDictionary alloc] init];
        
        for (int i = 4; i < stringLeng; i++)
        {
            float dataNum = bytes[i]&0xFF;
            NSString *dataNumStr = [NSString stringWithFormat:@"%.0f",dataNum];
            
            if (i%2 == 0)
            {
                [resultDic setObject:dataNumStr forKey:@"walkCounts"];
            }
            else
            {
                dataNumStr = [NSString stringWithFormat:@"%.1f",dataNum/10];
                [resultDic setObject:dataNumStr forKey:@"calorie"];
                NSInteger interval = 60*(i/2 - 2);
                NSDate *myDate = [handTime dateByAddingTimeInterval: interval];
                NSString *myDateStr = [myFormatter stringFromDate:myDate];
                [resultDic setObject:myDateStr forKey:@"createTime"];
                NSString *tipTime = [NSString stringWithFormat:@"%@00:00",[myDateStr substringToIndex:14]];
        
                [resultDic setObject:tipTime forKey:@"tipTime"];
                
                NSDictionary *dic = [[NSDictionary alloc] initWithDictionary:resultDic];
                [resultArr addObject:dic];
                [resultDic removeAllObjects];
            }
        }
    }
    else if ([mark isEqualToString:@"sleep"])
    {
        for (int i = 4; i < stringLeng; i++)
        {
            NSMutableDictionary *resultDic = [[NSMutableDictionary alloc] init];
            
            int dataNum = bytes[i]&0xFF;
            NSString *moveNum = [NSString stringWithFormat:@"%d",dataNum];
            [resultDic setObject:moveNum forKey:@"moveCounts"];
            
            NSInteger interval = 5*60*(i - 4);
            NSDate *myDate = [handTime dateByAddingTimeInterval: interval];
            NSString *myDateStr = [myFormatter stringFromDate:myDate];
            [resultDic setObject:myDateStr forKey:@"createTime"];
            
            [resultArr addObject:resultDic];
        }
    }
    else if ([mark isEqualToString:@"xinLv"])
    {
        for (int i = 4; i < stringLeng; i++)
        {
            NSMutableDictionary *resultDic = [[NSMutableDictionary alloc] init];
            
            int dataNum = bytes[i]&0xFF;
            NSString *xinLvNum = [NSString stringWithFormat:@"%d",dataNum];
            [resultDic setObject:xinLvNum forKey:@"heartRateNum"];
            
            NSInteger interval = 2 * (i - 4);
            NSDate *myDate = [handTime dateByAddingTimeInterval: interval];
            NSString *myDateStr = [myFormatter stringFromDate:myDate];
            [resultDic setObject:myDateStr forKey:@"sportTime"];
            
            [resultArr addObject:resultDic];
        }
    }
    
    return resultArr;
}

/***************************************************************************
 * 方法名称：getNowDateFromatAnDate
 * 功能描述：获取国际时间差值
 * 输入参数：
 * 返回数据：
 ***************************************************************************/
- (int)getNowDateFromatAnDate:(NSDate *)anyDate
{
    //设置源日期时区
    NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"];//或GMT
    //设置转换后的目标日期时区
    NSTimeZone* destinationTimeZone = [NSTimeZone localTimeZone];
    //得到源日期与世界标准时间的偏移量
    NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:anyDate];
    //目标日期与本地时区的偏移量
    NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:anyDate];
    //得到时间偏移量的差值
    NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;
    
    return interval;
}


@end
